<?php

/**
 * 会员中心
 */
namespace User\Controller;
use Common\Controller\MemberbaseController;
class SixingController extends MemberbaseController {
	
	protected $users_model;
	function _initialize(){
		parent::_initialize();
		$this->users_model=D("Common/Users");
		$userid=sp_get_current_userid();
		$user=$this->users_model->where(array("id"=>$userid))->find();
		 if($user['user_pass']==null){
			/* $this->display(':wangshang');
			die; */
		} 
	}
    //私信列表

	public function index() {
		$userid=sp_get_current_userid();

		$user=$this->users_model->where(array("id"=>$userid))->find();
		$sxgroup=M('sxgroup');
		$where['u_id']  =$userid ;
		$where['y_id']  = $userid;
		$where['_logic'] = 'or';
		$sx_list_c=$sxgroup->where($where)->count();
		$pagetpl='{first}{prev}{liststart}{list}{listend}{next}{last}';
		import('Page');
		$pagesize = 5;//设置每页数据数量
		$PageParam = C("VAR_PAGE");
		$page = new \Page($sx_list_c,$pagesize);
		$page->setLinkWraper("li");
		$page->__set("PageParam", $PageParam);
		$page->SetPager('default', $pagetpl, array("listlong" => "9", "first" => "首页", "last" => "尾页", "prev" => "上一页", "next" => "下一页", "list" => "*"));
		
		$sx_list=$sxgroup->where($where)->limit($page->firstRow . ',' . $page->listRows)->select();
		
		$page=$page->show('default');
		foreach($sx_list as $k=>$v){
			$rs=M('sx')->where(array('g_id'=>$v['id']))->order('sx_time desc')->limit(1)->select();
			$rs_c=M('sx')->where(array('g_id'=>$v['id'],'status'=>1,'y_id'=>$userid ))->count();
			if($v['u_id']==$userid){
				$dd_id=$v['y_id'];
			}else{
				$dd_id=$v['u_id'];
			}
			$user=$this->users_model->where(array("id"=>$dd_id))->field('user_nicename,avatar')->find();
			$sx_list[$k]['content']=$rs;
			$sx_list[$k]['count']=$rs_c;
			$sx_list[$k]['user']=$user;
		}
		
		$this->assign("Page", $page);//模版赋值
		$this->assign('sx_list',$sx_list);
		$this->assign($user);
    	$this->display(':sixing');
    }
	public function add() {
		if($_POST){
			$data=$_POST;
			$data['y_name']=htmlspecialchars($data['y_name']);
		
			$data['sx_content']=htmlspecialchars($data['sx_content']);
			
			$y_name=$data['y_name'];
			$rs=M('users')->where(array('user_nicename'=>$y_name))->find();
			
			
			$f_fs=M('sxgroup')->where(array('u_id'=>$data['u_id'],'y_id'=>$rs['id']))->find();
			if($f_fs==null){
				$f_fs=M('sxgroup')->where(array('u_id'=>$rs['id'],'y_id'=>$data['u_id']))->find();
			}
			if($data['sx_content']==null){
			
				echo "<span style='display: block;float: left;width: 20px;background: #f00;height: 20px;margin: 15px 6px 0 89px;border-radius: 11px;line-height: 17px;font-size: 15px;color: #fff;    text-align: center;'>x</span>请输入您要私信的内容！";
				die;
			}
			if($rs==null){
				//$this->error("该用户不存在");
				echo "<span style='display: block;float: left;width: 20px;background: #f00;height: 20px;margin: 15px 6px 0 89px;border-radius: 11px;line-height: 17px;font-size: 15px;color: #fff;    text-align: center;'>x</span>该用户不存在！";
				die;
			}else if($rs['id']==$data['u_id']){
				//$this->error("不能与自己对话");
				echo "<span style='display: block;float: left;width: 20px;background: #f00;height: 20px;margin: 15px 6px 0 89px;border-radius: 11px;line-height: 17px;font-size: 15px;color: #fff;    text-align: center;'>x</span>不能与自己对话！";
				die;
			}else if($f_fs!=null){
				$sx['y_id']=$rs['id'];
				$sx['g_id']=$f_fs['id'];
				$sx['content']=$data['sx_content'];
				$sx['sx_time']=time();
				$sx_rs=M('sx')->add($sx);
			}else{
				$sxgroup['u_id']=$data['u_id'];
				$sxgroup['y_id']=$rs['id'];
				$sxg_rs=M('sxgroup')->add($sxgroup);
				
				$sx_l['y_id']=$rs['id'];
				$sx_l['g_id']=(int)$sxg_rs;					
				$sx_l['content']=$data['sx_content'];
				$sx_l['sx_time']=time();
				$sx_rs=M('sx')->add($sx_l);
			}
			if($sx_rs){
				//$this->success("发送成功");
				echo "<img src='/themes/wtx/Public/images/success.png' width='19' height='18'  />发送成功";
				die;
			}else{
				//$this->error("发送失败");
				echo "<span style='display: block;float: left;width: 20px;background: #f00;height: 20px;margin: 15px 6px 0 89px;border-radius: 11px;line-height: 17px;font-size: 15px;color: #fff;    text-align: center;'>x</span>发送失败";
				die;
			}
		}
    }
	public function s_user() {
		if($_POST){
			$s_user=htmlspecialchars($_POST['s_uer']);
			
			$map['user_nicename'] = array('like', '%'.$s_user.'%');
			if($s_user){
				$rs=M('users')->where($map)->limit(10)->select();
			}
			
			if($rs){
					$str="<ul>";
					foreach($rs as $v){
						if($v['avatar']==null){
							$v['avatar']="/data/upload/ueditor/20160509/57305e0400e81.png";
						}else{
							$v['avatar']=sp_get_user_avatar_url($v['avatar']);
						}
						$str.="<li class='s_li' onclick='copyText("
						.'"'.$v['user_nicename'].'"'.
						")'><img src='"					
						.$v['avatar'].
						"'class='s_touxiang'/><span class='s_name'>"
						.$v['user_nicename'].
						"</span><span class='s_de'>"
						.$v['signature'].
						"</span></li>";
					}
					$str.="</ul>";
					echo $str;
			}else{
				echo "<span class='wyh'>没有匹配到的用户</span>";
			}
		
		}
    }
	public function del() {
		if($_GET){
			$id=I('id');
			$sx=M('sx');
			$sxgroup=M('sxgroup');
			$sxgroup->where(array('id'=>$id))->delete();
			$sx->where(array('g_id'=>$id))->delete();
			$this->redirect('user/sixing/index', array(), 0, '页面跳转中...');
			
		}
    }
	public function conversation() {
		if($_GET){
			$id=I('id');
			$sx=M('sx');
			
			
			$sx_list_c=$sx->where(array('g_id'=>$id))->order('sx_time desc')->count();
			$pagetpl='{first}{prev}{liststart}{list}{listend}{next}{last}';
			import('Page');
			$pagesize = 10;//设置每页数据数量
			$PageParam = C("VAR_PAGE");
			$page = new \Page($sx_list_c,$pagesize);
			$page->setLinkWraper("li");
			$page->__set("PageParam", $PageParam);
			$page->SetPager('default', $pagetpl, array("listlong" => "9", "first" => "首页", "last" => "尾页", "prev" => "上一页", "next" => "下一页", "list" => "*"));
			$rs=$sx->where(array('g_id'=>$id))->limit($page->firstRow . ',' . $page->listRows)->order('sx_time desc')->select();
			$page=$page->show('default');
			$userid=sp_get_current_userid();
				foreach($rs as $k=>$v){
					$f_fs=M('sxgroup')->where(array('id'=>$id))->find();
					if($v['y_id']==$f_fs['u_id']){
						$d_id=$f_fs['y_id'];
					}else{
						$d_id=$f_fs['u_id'];
					}
					$user=$this->users_model->where(array("id"=>$d_id))->field('id,user_nicename,avatar')->find();
					$rs[$k]['user']=$user;
				}
			//回复功能			
			$hf_fs=M('sxgroup')->where(array('id'=>$id))->find();
			if($userid==$hf_fs['u_id']){
				$hf_id=$hf_fs['y_id'];
			}else{
				$hf_id=$hf_fs['u_id'];
			}
			$hf_user=$this->users_model->where(array("id"=>$hf_id))->field('id,user_nicename,avatar')->find();
			$where['g_id']=$id;
			$where['y_id']=$userid;
			$data['status'] = 0;
			M('sx')->where($where)->data($data)->save();
			$this->assign("hf_user", $hf_user);//模版赋值
			$this->assign("Page", $page);//模版赋值
			$this->assign("conversation", $rs);//模版赋值
			$this->display(':conversation');
		}
	}
}

